<?php
// +----------------------------------------------------------------------
// |店铺分类管理
// +----------------------------------------------------------------------
// |Data:2016-06-24 下午3:31
// +----------------------------------------------------------------------
// |Author: 吕志雄 <870923001@qq.com>
// +----------------------------------------------------------------------
namespace Admin\Controller;
use Common\Controller\AdminCommonController;

class ShopCateController extends AdminCommonController{
    private $_model;
    protected function _initialize(){
        parent::_initialize();
        $this->_model = D('Admin/ShopCate');
    }

    public function index(){
        $list = $this->_model->getAll();
        $this->assign('list',$list);
        $this->display();
    }

    public function edit(){
        $id = I('get.id');
        if($id){
            $detail = $this->_model->detail($id);
            $this->assign('detail',$detail);
        }
        $this->display();
    }

    public function update(){
        $id = I('post.id');
        $title = I('post.title');
        empty($title) && $this->returnData('请选择操作项!');
        if(abslength($title) > 30) $this->returnData('分类名称长度1~30个字符!');
        $data = array(
            'title'=>$title,
        );
        if($id){
            $data['id'] = $id;
            $data['edit_time'] = time();
        }else{
            $data['add_time'] = time();
        }
        $msg = $id ? '编辑' : '添加';
        $result = $this->_model->update($data);
        if($result){
            $this->addHistoryLog($msg.'商品分类成功');
            $this->returnData($msg.'成功!',1);
        }else{
            $this->returnData($msg.'失败!');
        }
    }

    public function del(){
        $id = I('get.id');
        empty($id) && $this->returnData('请选择操作项!');
        $is_has = D('Common/Shop')->hasCate($id);
        !empty($is_has) && $this->returnData('该分类已被使用,不能删除!');
        $result = $this->_model->del($id);
        if($result){
            $this->addHistoryLog('删除商品分类成功');
            $this->returnData('删除成功!',1);
        }else{
            $this->returnData('删除失败!');
        }
    }
}
?>